package common;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import lombok.extern.log4j.Log4j;

@SuppressWarnings("unchecked")
public class HisConfig {
		//<editor-fold defaultstate="collapsed" desc="=========================== 系统公共基本参数 =========================================">
		// 根路径
		public static String WebContentPath = Thread.currentThread().getContextClassLoader().getResource("").toString().replace("lib/", "").replace("WEB-INF/","").replace("file:/","").replace("classes/","").replace("%20"," ").replace("build", "WebContent");
		
		// 主配置路径(D:/ProgramFiles/j2EE-eclipse/2016Workspace/AlipayWebpro/WebContent//WEB-INF/gg_config/)
		public static String Main_Path = WebContentPath + "webapps/baas/WEB-INF/gg_config/";
		static{
			System.out.println(Main_Path);
		}
		// 与his交互的网关地址
		public static String HisGateWayUrl = "";
		// 与his交互的网关超时时间（毫秒）
		public static int HisGateWayTimeout = 0;
	    // 保存系统定义的错误码及错误信息
	    public static Map<String, String> HisErrorCodeMap = new HashMap<String, String>();
	    // 与his交互的签名Key
	    public static String HIS_MD5KEY = "HtczW2pVi22aibK1HVfoTSz8KGEyCdiT";
	    
	    // 与App管理服务交互的网关地址
	 	public static String AppGateWayUrl = "";
	 	// 与App管理服务的网关超时时间（毫秒）
	 	public static int AppGateWayTimeout = 0;
	 	// 保存系统定义的错误码及错误信息
	 	public static Map<String, String> AppErrorCodeMap = new HashMap<String, String>();
	    
	    // 与Wx交互的网关地址
	    public static String WxGateWayUrl = "";
	    // 与Wx交互的网关超时时间（毫秒）
	    public static int WxGateWayTimeout = 0;
	    // 与Wx保存系统定义的错误码及错误信息
	    public static Map<String, String> WxErrorCodeMap = new HashMap<String, String>();
	    // 与Wx交互的签名Key
	    public static String WX_MD5KEY = "9rpJyxfDvSY6z3cVhyd4Ku9B4sfWiVbz";
	    
	    // 与Ali交互的网关地址
	    public static String AliGateWayUrl = "";
	    // 与Ali交互的网关超时时间（毫秒）
	    public static int AliGateWayTimeout = 0;
	    // 与Ali保存系统定义的错误码及错误信息
	    public static Map<String, String> AliErrorCodeMap = new HashMap<String, String>();
	    
	    // 与WxEbms交互的网关地址
	    public static String WxEbmsGateWayUrl = "";
	    // 与WxEbms交互的网关超时时间（毫秒）
	    public static int WxEbmsGateWayTimeout = 0;
	    // 与WxEbms保存系统定义的错误码及错误信息
	    public static Map<String, String> WxEbmsErrorCodeMap = new HashMap<String, String>();
	    // 与WxEbms交互的签名Key
	    public static String WxEbms_MD5KEY = "ErThh9yIyTjVQKAT14mMZExI7NIqzNYx";
	    //</editor-fold> 
	    
	    //<editor-fold>
	    // 百度AI的appid
	    public static final String BaiDuAI_APP_ID = "10206414";
	    // 百度AI的API_KEY
		public static final String BaiDuAI_API_KEY = "mRUcmP2nmhl1AqqQ8VB4fFSY";
		// 百度AI的SECRET_KEY
		public static final String BaiDuAI_SECRET_KEY = "Ypiv27b3CwDHdwHGhhTETZhqn4qmx5o1";
	    //</editor-fold>
	    
	    //<editor-fold defaultstate="collapsed" desc="=========================== 日志基本参数 =========================================">
	    // 专用于输出到控制台的日志
	    public final static Logger Log_Console;

	    // 系统状态级别的日志
	    public final static Logger Log_System;

	    // 记录每日的交易报文日志(与C端)
	    public final static Logger Log_DailyTrans;

	    // 记录每日与阿里端交易报文日志(与阿里端)
	    public final static Logger Log_Ali;
	    
	    // 记录每日与微信端交易报文日志(与微信端)
	    public final static Logger Log_Wx;
	    
	    // 记录与HIS服务网关的通信日志
	    public final static Logger Log_His;
	    // 记录与WxEbms服务网关的通讯日志
	    public final static Logger Log_WxEbms;
	    //</editor-fold>  
	  
		
		
	    static {
	        System.out.println("======================= Baas HisConfig 初始化 Begin ========================");

	        // 设置系统属性，主要用于Log4j里取到记录日志的相对路径
	        System.setProperty("self.dir", Main_Path);
	        
	        // 初始化日志操作类
	        DOMConfigurator.configure(Main_Path + "/log4j.xml");
	        Log_Console = Logger.getLogger("Log.Console");
	        Log_System = Logger.getLogger("Log.System");
	        Log_DailyTrans = Logger.getLogger("Log.DailyTrans");
	        Log_Ali = Logger.getLogger("Log.Alipay");
	        Log_Wx = Logger.getLogger("Log.WeiXin");
	        Log_His = Logger.getLogger("Log.His");
	        Log_WxEbms = Logger.getLogger("Log.WxEbms");
	        
	        InputStream configFile = HisConfig.class.getResourceAsStream("hisApp.config.xml");
	        try{
				SAXReader reader = new SAXReader();
				Document doc = reader.read(configFile);
				Element root = doc.getRootElement();
				// HIS配置文件数据初始化===START
				Element cfg = root.element("HisConfig");
				HisGateWayUrl = cfg.elementTextTrim("GateWay");
				HisGateWayTimeout = Integer.parseInt(cfg.elementTextTrim("timeout"));
				List<Element> list = (List<Element>)cfg.elements();
				for(Element e: list){
					HisErrorCodeMap.put(e.getName(), e.getTextTrim());
				}
				// HIS配置文件数据初始化===END
				
				// App管理服务配置文件数据初始化===START
				cfg = root.element("AppConfig");
				AppGateWayUrl = cfg.elementTextTrim("GateWay");
				AppGateWayTimeout = Integer.parseInt(cfg.elementTextTrim("timeout"));
				list = (List<Element>)cfg.elements();
				for(Element e: list){
					AppErrorCodeMap.put(e.getName(), e.getTextTrim());
				}
				// App管理服务配置文件数据初始化===END
				
				// 国光微信网关配置文件数据初始化===START
				cfg = root.element("WxConfig");
				WxGateWayUrl = cfg.elementTextTrim("GateWay");
				WxGateWayTimeout = Integer.parseInt(cfg.elementTextTrim("timeout"));
				list = (List<Element>)cfg.elements();
				for(Element e: list){
					WxErrorCodeMap.put(e.getName(), e.getTextTrim());
				}
				// 国光微信网关配置文件数据初始化===END
				
				// 国光支付宝网关配置文件数据初始化===START
				cfg = root.element("AliConfig");
				AliGateWayUrl = cfg.elementTextTrim("GateWay");
				AliGateWayTimeout = Integer.parseInt(cfg.elementTextTrim("timeout"));
				list = (List<Element>)cfg.elements();
				for(Element e: list){
					AliErrorCodeMap.put(e.getName(), e.getTextTrim());
				}
				// 国光支付宝网关配置文件数据初始化===END
				
				// 国光WxEbms网关配置文件数据初始化===START
				cfg = root.element("WxEbmsConfig");
				WxEbmsGateWayUrl = cfg.elementTextTrim("GateWay");
				WxEbmsGateWayTimeout = Integer.parseInt(cfg.elementTextTrim("timeout"));
				list = (List<Element>)cfg.elements();
				for(Element e: list){
					WxEbmsErrorCodeMap.put(e.getName(), e.getTextTrim());
				}
				// 国光WxEbms网关配置文件数据初始化===END
			}catch(Exception e){
				e.printStackTrace();
			}
	        System.out.println("======================= Baas HisConfig 初始化 End ========================");
	    }
		
}
